/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package de.hsmannheim.pepa.patientenportal.apanwep.model;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;

import de.hsmannheim.pepa.patientenportal.apanwep.service.ClpSerializer;
import de.hsmannheim.pepa.patientenportal.apanwep.service.contactLocalServiceUtil;

import java.io.Serializable;

import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Cedric Nana
 */
public class contactClp extends BaseModelImpl<contact> implements contact {
	public contactClp() {
	}

	@Override
	public Class<?> getModelClass() {
		return contact.class;
	}

	@Override
	public String getModelClassName() {
		return contact.class.getName();
	}

	@Override
	public long getPrimaryKey() {
		return _message_id;
	}

	@Override
	public void setPrimaryKey(long primaryKey) {
		setMessage_id(primaryKey);
	}

	@Override
	public Serializable getPrimaryKeyObj() {
		return _message_id;
	}

	@Override
	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey(((Long)primaryKeyObj).longValue());
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("message_id", getMessage_id());
		attributes.put("user_id", getUser_id());
		attributes.put("datetime", getDatetime());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		Long message_id = (Long)attributes.get("message_id");

		if (message_id != null) {
			setMessage_id(message_id);
		}

		Long user_id = (Long)attributes.get("user_id");

		if (user_id != null) {
			setUser_id(user_id);
		}

		String datetime = (String)attributes.get("datetime");

		if (datetime != null) {
			setDatetime(datetime);
		}
	}

	@Override
	public long getMessage_id() {
		return _message_id;
	}

	@Override
	public void setMessage_id(long message_id) {
		_message_id = message_id;

		if (_contactRemoteModel != null) {
			try {
				Class<?> clazz = _contactRemoteModel.getClass();

				Method method = clazz.getMethod("setMessage_id", long.class);

				method.invoke(_contactRemoteModel, message_id);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getUser_id() {
		return _user_id;
	}

	@Override
	public void setUser_id(long user_id) {
		_user_id = user_id;

		if (_contactRemoteModel != null) {
			try {
				Class<?> clazz = _contactRemoteModel.getClass();

				Method method = clazz.getMethod("setUser_id", long.class);

				method.invoke(_contactRemoteModel, user_id);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getDatetime() {
		return _datetime;
	}

	@Override
	public void setDatetime(String datetime) {
		_datetime = datetime;

		if (_contactRemoteModel != null) {
			try {
				Class<?> clazz = _contactRemoteModel.getClass();

				Method method = clazz.getMethod("setDatetime", String.class);

				method.invoke(_contactRemoteModel, datetime);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public BaseModel<?> getcontactRemoteModel() {
		return _contactRemoteModel;
	}

	public void setcontactRemoteModel(BaseModel<?> contactRemoteModel) {
		_contactRemoteModel = contactRemoteModel;
	}

	public Object invokeOnRemoteModel(String methodName,
		Class<?>[] parameterTypes, Object[] parameterValues)
		throws Exception {
		Object[] remoteParameterValues = new Object[parameterValues.length];

		for (int i = 0; i < parameterValues.length; i++) {
			if (parameterValues[i] != null) {
				remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
			}
		}

		Class<?> remoteModelClass = _contactRemoteModel.getClass();

		ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

		Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

		for (int i = 0; i < parameterTypes.length; i++) {
			if (parameterTypes[i].isPrimitive()) {
				remoteParameterTypes[i] = parameterTypes[i];
			}
			else {
				String parameterTypeName = parameterTypes[i].getName();

				remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
			}
		}

		Method method = remoteModelClass.getMethod(methodName,
				remoteParameterTypes);

		Object returnValue = method.invoke(_contactRemoteModel,
				remoteParameterValues);

		if (returnValue != null) {
			returnValue = ClpSerializer.translateOutput(returnValue);
		}

		return returnValue;
	}

	@Override
	public void persist() throws SystemException {
		if (this.isNew()) {
			contactLocalServiceUtil.addcontact(this);
		}
		else {
			contactLocalServiceUtil.updatecontact(this);
		}
	}

	@Override
	public contact toEscapedModel() {
		return (contact)ProxyUtil.newProxyInstance(contact.class.getClassLoader(),
			new Class[] { contact.class }, new AutoEscapeBeanHandler(this));
	}

	@Override
	public Object clone() {
		contactClp clone = new contactClp();

		clone.setMessage_id(getMessage_id());
		clone.setUser_id(getUser_id());
		clone.setDatetime(getDatetime());

		return clone;
	}

	@Override
	public int compareTo(contact contact) {
		long primaryKey = contact.getPrimaryKey();

		if (getPrimaryKey() < primaryKey) {
			return -1;
		}
		else if (getPrimaryKey() > primaryKey) {
			return 1;
		}
		else {
			return 0;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!(obj instanceof contactClp)) {
			return false;
		}

		contactClp contact = (contactClp)obj;

		long primaryKey = contact.getPrimaryKey();

		if (getPrimaryKey() == primaryKey) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return (int)getPrimaryKey();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(7);

		sb.append("{message_id=");
		sb.append(getMessage_id());
		sb.append(", user_id=");
		sb.append(getUser_id());
		sb.append(", datetime=");
		sb.append(getDatetime());
		sb.append("}");

		return sb.toString();
	}

	@Override
	public String toXmlString() {
		StringBundler sb = new StringBundler(13);

		sb.append("<model><model-name>");
		sb.append("de.hsmannheim.pepa.patientenportal.apanwep.model.contact");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>message_id</column-name><column-value><![CDATA[");
		sb.append(getMessage_id());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>user_id</column-name><column-value><![CDATA[");
		sb.append(getUser_id());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>datetime</column-name><column-value><![CDATA[");
		sb.append(getDatetime());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private long _message_id;
	private long _user_id;
	private String _datetime;
	private BaseModel<?> _contactRemoteModel;
}